﻿using Cognex.VisionPro;
using Cognex.VisionPro.CalibFix;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows;
using YJYpublicClass;

namespace WindowsFormsApp2.JiayuModelset
{
    public class ModelCaozuo
    {

        //一些视觉操作也可以在这里完成
        //  public CogAcqFifoTool FifoAcq_tool = null;
        //专门处理模型操作的
        public JayuYindaomodel modelone = new JayuYindaomodel();
        public ICogRecord disprecord1 = null;
        public ICogRecord disprecord2 = null;
        public ICogRecord disprecord3 = null;
        public ICogRecord disprecord4 = null;


        //记录量角度
        public int _LOKNG = 0;
        public int _ROKNG = 0;
        //保存
        public ResultFunc Savemode(string path, JayuYindaomodel model)
        {
            try
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, model);
                stream.Close();
                return new ResultFunc(Funstate.OK, "");
            }
            catch (Exception ex)
            {
                return new ResultFunc(Funstate.异常, ex.ToString());
            }
        }
        //加载
        public ResultFunc Loadmode(string path, out JayuYindaomodel model)
        {
            try
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
                model = (JayuYindaomodel)formatter.Deserialize(stream);
                stream.Close();
                //模块赋值
                ResultFunc reslut = model.init();
                if (reslut.State == Funstate.OK)
                {
                    return new ResultFunc(Funstate.OK, "");
                }
                else
                {
                    return new ResultFunc(Funstate.异常, reslut.mes);
                }
            }
            catch (Exception ex)
            {
                model = new JayuYindaomodel();
                return new ResultFunc(Funstate.异常, ex.ToString());
            }
        }

        public void setconfig()
        {
            Frm_模型操作 tempwindow = new Frm_模型操作(this);
            tempwindow.Show();
        }


        #region 检测流程

        //这个初始就是单纯的



        public ResultFunc[] _上工位检测()
        {
            ResultFunc[] result = new ResultFunc[2] { new ResultFunc(), new ResultFunc()};
            try
            {

                //大孔修正
                result[0].mes = result[0].mes + "检测";
                result[1].mes = result[1].mes + "检测";
                modelone._上工位检测工具.Run();

                int OKNG_1 = (int)modelone._上工位检测工具.Outputs["OKNG_1"].Value;
                int OKNG_2 = (int)modelone._上工位检测工具.Outputs["OKNG_2"].Value;
                disprecord1 = modelone._上工位检测工具.CreateLastRunRecord().SubRecords[modelone._上1显示图片索引];
                disprecord2 = modelone._上工位检测工具.CreateLastRunRecord().SubRecords[modelone._上2显示图片索引];


                if (modelone._上工位检测工具.RunStatus.Result == CogToolResultConstants.Accept)   //成功走完整个流程
                {
                    
                    if (OKNG_1 == 1)
                    {
                        result[0].State = Funstate.OK;
                    }
                    else
                    {
                        result[0].State = Funstate.异常;
                    }
                    result[0].mes = result[0].mes + "检测完成";

                    if (OKNG_2 == 1)
                    {
                        result[1].State = Funstate.OK;
                    }
                    else
                    {
                        result[1].State = Funstate.异常;
                    }
                    result[1].mes = result[1].mes + "检测完成";


                    return result;
                }
                else
                {
                    result[0].State = Funstate.异常;
                    result[1].State = Funstate.异常;
                    return result;
                }
                // result.mes = result.mes + "检测结果" + (OKNG == 1 ? "OK" : "NG") + "\r\n";
                // return result;
            }
            catch (Exception ex)
            {
                return result;
            }

        }





        public ResultFunc[] _下工位检测()
        {
            ResultFunc[] result = new ResultFunc[2] { new ResultFunc(), new ResultFunc() }; 
            try
            {

                //大孔修正
                result[0].mes = result[0].mes + "检测";
                result[1].mes = result[1].mes + "检测";

                modelone._下工位检测工具.Run();
                int OKNG_1 = (int)modelone._下工位检测工具.Outputs["OKNG_1"].Value;
                int OKNG_2 = (int)modelone._下工位检测工具.Outputs["OKNG_2"].Value;
                disprecord3 = modelone._下工位检测工具.CreateLastRunRecord().SubRecords[modelone._下1显示图片索引];
                disprecord4 = modelone._下工位检测工具.CreateLastRunRecord().SubRecords[modelone._下2显示图片索引];


                if (modelone._下工位检测工具.RunStatus.Result == CogToolResultConstants.Accept)   //成功走完整个流程
                {
                    
                    if (OKNG_1 == 1)
                    {
                        result[0].State = Funstate.OK;
                    }
                    else
                    {
                        result[0].State = Funstate.异常;
                    }
                    result[0].mes = result[0].mes + "检测完成";

                    if (OKNG_2 == 1)
                    {
                        result[1].State = Funstate.OK;
                    }
                    else
                    {
                        result[1].State = Funstate.异常;
                    }
                    result[1].mes = result[1].mes + "检测完成";


                    return result;
                }
                else
                {
                    result[0].State = Funstate.异常;
                    result[1].State = Funstate.异常;
                    return result;
                }
                // result.mes = result.mes + "检测结果" + (OKNG == 1 ? "OK" : "NG") + "\r\n";
                // return result;
            }
            catch (Exception ex)
            {
                return result;
            }

        }





        #endregion



    }
}
